//
//  GQHFreeView.m
//  Xanthium
//
//  Created by GuanQinghao on 14/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHFreeView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHFreeView () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate> {
    
    NSArray *titleArray;
    NSArray *_placeHolderArray;
}

@end


@implementation GQHFreeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"起始地",@"目的地",@"可载货物",@"车辆载重",@"备注"];
    
    if (!_qh_contentArray) {
        
        _qh_contentArray = [NSMutableArray arrayWithArray:@[@"请选择",@"请选择",@"",@"",@""]];
    }
    _placeHolderArray = @[@"",@"",@"可不填",@"可不填",@"请填写备注信息"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
    }
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.perLabel_width.constant = 0;
    if (indexPath.row == 3)
    {
        cell.enterText.keyboardType = UIKeyboardTypeDecimalPad;
    }
    if (indexPath.row < 2)
    {
        cell.isEnter = NO;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.showValueLabel.text = _qh_contentArray[indexPath.row];
    }
    else
    {
        if (indexPath.row == 3)
        {
            cell.enterText.keyboardType = UIKeyboardTypeDecimalPad;
        }
        cell.isEnter = YES;
        cell.enterText.text = _qh_contentArray[indexPath.row];
        cell.enterText.placeholder = _placeHolderArray[indexPath.row];
        cell.enterText.tag = 10 + indexPath.row;
        cell.enterText.delegate = self;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {

        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag - 10 withObject:textField.text];
}

@end
